/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pagoda.engine;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Kevin Krohe
 */
public abstract class BaseTestActionContainer
        extends BaseTestAction
        implements TestActionContainer
{

    private List<TestAction> _children
            = new LinkedList<TestAction>();

    private boolean _continueOnError;

    public TestActionContainer addSubAction(TestAction action)
    {
        int count = _children.size();
        String path = String.format("%s.%d", getPath(), count);
        _children.add(
                action.setParent(this)
                .setPath(path)
                );
        return this;
    }

    public TestActionContainer setContinueOnErorr(boolean doContinue)
    {
        _continueOnError = doContinue; return this;
    }

    public boolean getContinueOnError()
    {
        return _continueOnError;
    }

    protected Iterable<TestAction> getChildren()
    {
        return _children;
    }

    @Override
    protected TestActionResult performAction() {
        return executeChildren(getContinueOnError());
    }

        public TestActionResult executeChildren(boolean continueOnError)
    {
        TestActionResult result = new TestActionResult();
        for(TestAction child : getChildren())
        {
            TestActionResult r = child.execute();
            result.MergeSubResult(r);
            
            if(r.Status.compareTo(TestActionStatus.Error) == 0
                    && !getContinueOnError())
            {
                break;
            }
        }
        return result;
    }

}
